﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Khai báo đối tượng
            XmlTextWriter xml = new XmlTextWriter("TK8.xml",null);
            //Demo commit
            //root
            xml.WriteStartElement("UTEHY");
            //bản ghi 1
                xml.WriteStartElement("SinhVien");
                xml.WriteAttributeString("ID","SV01");
                xml.WriteStartElement("Ten");
                xml.WriteValue("Lê Trung Hiếu");
                xml.WriteEndElement();
                xml.WriteEndElement();

                xml.WriteStartElement("SinhVien");
                xml.WriteAttributeString("ID", "SV02");
                xml.WriteStartElement("Ten");
                xml.WriteValue("Nguyễn Mạnh Cường");
                xml.WriteEndElement();
                xml.WriteEndElement();

                xml.WriteStartElement("SinhVien");
                xml.WriteAttributeString("ID", "SV03");
                xml.WriteStartElement("Ten");
                xml.WriteValue("Dương Thành Đạo");
                xml.WriteEndElement();
                xml.WriteEndElement();
            //bản ghi 2
                xml.WriteStartElement("Lop");
                xml.WriteAttributeString("ID", "L01");
                xml.WriteStartElement("Tenlop");
                xml.WriteValue("TK8SE");
                xml.WriteEndElement();
                xml.WriteEndElement();
            //end root
            xml.WriteEndElement();

            xml.Close();
            MessageBox.Show("OK");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XmlTextWriter xml = new XmlTextWriter("TK8.xml", null);
            xml.WriteStartElement("TEXT");
            StreamReader sr = new StreamReader("TK8.txt");
            string dong = sr.ReadLine();
            while (dong != null)
            {
                string[] a = dong.Split(',');
                xml.WriteStartElement("SinhVien");
                    xml.WriteAttributeString("ID", a[0]);
                    xml.WriteStartElement("Ten");
                    xml.WriteValue(a[1]);
                    xml.WriteEndElement();
                xml.WriteEndElement();

                dong = sr.ReadLine();
            }
            xml.WriteEndElement();
            xml.Close();
            MessageBox.Show("OK");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strTK8 = "";
            string strTenElement = "";
            XmlTextReader docXML = new XmlTextReader("TK8.xml");
            while(docXML.Read())
            {
                switch (docXML.NodeType)
                {
                    case XmlNodeType.Element:
                        strTenElement=docXML.Name;
                        MessageBox.Show(strTenElement);
                        break;
                    case XmlNodeType.Text:
                        switch (strTenElement)
                        {
                            case "Ten":
                                strTK8 += docXML.Value;
                                strTK8 += ", ";
                                MessageBox.Show(strTK8);
                                break;
                        }
                        break;
                }
            }
        }
    }
}
